package me.jessyan.armscomponent.app.mvp.ui.activity.user;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.app.mvp.model.api.Api;
import me.jessyan.armscomponent.commonres.utils.ProgressDialogUtil;
import me.jessyan.armscomponent.commonres.widget.toast.ToastView;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;
import me.jessyan.armscomponent.app.R;
import me.jessyan.armscomponent.app.di.component.DaggerUserRegisterComponent;
import me.jessyan.armscomponent.app.mvp.contract.user.UserRegisterContract;
import me.jessyan.armscomponent.app.mvp.presenter.user.UserRegisterPresenter;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * <p>
 * Author: Taozebi
 * Date: 08/08/2021 15:36
 * Description: 用户注册
 */

@Route(path = RouterHub.USER_REGISTERACTIVITY)
public class UserRegisterActivity extends BaseActivity<UserRegisterPresenter> implements UserRegisterContract.View {


    @BindView(R.id.user_btn_register)
    Button user_btn_register;

    @BindView(R.id.user_et_register_username)
    EditText user_et_register_username;
    @BindView(R.id.user_et_register_password)
    EditText user_et_register_password;
    @BindView(R.id.user_et_register_password_comfirm)
    EditText user_et_register_password_comfirm;
    @BindView(R.id.user_et_register_image_code)
    EditText user_et_register_image_code;

    @BindView(R.id.user_tv_get_ver_code)
    TextView user_tv_get_ver_code;
    @BindView(R.id.user_et_register_ver_code)
    EditText user_et_register_ver_code;
    @BindView(R.id.user_et_register_invite_code)
    EditText user_et_register_invite_code;
    @BindView(R.id.user_iv_register_image_code)
    ImageView user_iv_register_image_code;


    @BindView(R.id.user_cb_agree)
    CheckBox user_cb_agree;

//    private ImageLoader mImageLoader;
    private String username;
    private String password;
    private String countryCode = "";

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerUserRegisterComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.user_activity_register; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

//        mImageLoader = ArmsUtils.obtainAppComponentFromContext(this).imageLoader();
//        mPresenter.imgCaptcha();

        initSpinner();
    }

    @Override
    public void showLoading() {
        ProgressDialogUtil.show(this,"","加载中...");
    }

    @Override
    public void hideLoading() {
        ProgressDialogUtil.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ToastView.getInstance().showToast(this,message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @OnClick({R.id.user_iv_register_image_code,R.id.user_tv_get_ver_code,R.id.user_btn_register,R.id.user_tv_login_mobile,R.id.user_tv_to_login,
            R.id.user_tv_user_agreement,R.id.user_tv_privacy_clause})
    public void onClicked(View v) {
        int id = v.getId();
        if (id == R.id.user_iv_register_image_code) {
            if (Utils.isFastClick()) {
                mPresenter.imgCaptcha();
            }
        }else if (id == R.id.user_tv_get_ver_code) {
            if (Utils.isFastClick()) {
                mPresenter.checkMobile();
            }
        }else if (id == R.id.user_btn_register) {
            if (Utils.isFastClick()) {
                mPresenter.next();
            }
        } else if (id == R.id.user_tv_to_login) {
            if (Utils.isFastClick()) {
                toLogin(true);
            }
        } else if (id == R.id.user_tv_login_mobile) {
            //手机号登录
            if (Utils.isFastClick()) {
                toLogin(false);
            }
        } else if (id == R.id.user_tv_user_agreement) {
            if (Utils.isFastClick()) {
                ARouter.getInstance().build(RouterHub.APP_COMMONWEBVIEWACTIVITY)
                        .withString("title","用户协议")
                        .withString("url", Api.APP_DOMAIN_USER_AGREEMENT)
                        .navigation();
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        } else if (id == R.id.user_tv_privacy_clause) {
            //隐私条款
            if (Utils.isFastClick()) {
                ARouter.getInstance().build(RouterHub.APP_COMMONWEBVIEWACTIVITY)
                        .withString("title","隐私条款")
                        .withString("url", Api.APP_DOMAIN_PRIVACY_CLAUSE)
                        .navigation();
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        }
    }

    private void initSpinner() {
        String[] countryCodeArray = getResources().getStringArray(R.array.public_CountryCodes);
        //声明一个下拉列表的数组适配器
        ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(this, R.layout.layout_item_select, countryCodeArray);
        //设置数组适配器的布局样式
        starAdapter.setDropDownViewResource(R.layout.layout_item_drapdown);
        //从布局文件中获取名叫sp_dialog的下拉框
        Spinner sp = findViewById(R.id.user_spinner_code);
        //设置下拉框的标题，不设置就没有难看的标题了
        sp.setPrompt("选择国家代码");
        //设置下拉框的数组适配器
        sp.setAdapter(starAdapter);
        //设置下拉框默认的显示第一项
        sp.setSelection(0);
        //给下拉框设置选择监听器，一旦用户选中某一项，就触发监听器的onItemSelected方法
        sp.setOnItemSelectedListener(new MySelectedListener());
    }

    class MySelectedListener implements AdapterView.OnItemSelectedListener {

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            String[] countryCodeArray = getResources().getStringArray(R.array.public_CountryCodes);
//            showMessage(countryCodeArray[i]);
            countryCode = countryCodeArray[i];
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    }

    private void toLogin(boolean isPasswordLogin) {
        ARouter.getInstance().build(RouterHub.USER_LOGINACTIVITY)
                .withBoolean("isPasswordLogin",isPasswordLogin)
                .navigation();
        finish();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }



    @Override
    public UserRegisterActivity getActivity() {
        return this;
    }

    @Override
    public String getUserName() {
        return /*phonePreff +*/ user_et_register_username.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return user_et_register_password.getText().toString().trim();
    }

    @Override
    public String getPasswordComfirm() {
        return user_et_register_password_comfirm.getText().toString().trim();
    }

    @Override
    public String getImaCaptcha() {
        return user_et_register_image_code.getText().toString().trim();
    }

    @Override
    public String getSmsCaptcha() {
        return user_et_register_ver_code.getText().toString().trim();
    }

    @Override
    public String getInviteCode() {
        return user_et_register_invite_code.getText().toString().trim();
    }

    @Override
    public boolean isUserAagree() {
        return user_cb_agree.isChecked();
    }

    @Override
    public boolean comfirmPassword() {
        return getPassword().equals(getPasswordComfirm());
    }

    @Override
    public void onImageCap(Bitmap bitmap) {
        user_iv_register_image_code.setImageBitmap(bitmap);

    }

    @Override
    public void getVerCodeUnable(boolean enable, String tips) {
        user_tv_get_ver_code.setEnabled(enable);
        user_tv_get_ver_code.setTextColor(enable?Color.parseColor("#49BA2C"):getResources().getColor(R.color.public_contentTextColorGray));
        user_tv_get_ver_code.setText(tips);
    }


    @Override
    public String getCountryCode() {
        return countryCode;
    }
}
